package kgame5.pbe.atlantic.comp {
import kgame5.pbe.atlantic.fsm.FSM;
import kgame5.pbe.atlantic.fsm.FollowingStateParams;
import kgame5.pbe.atlantic.fsm.GState;
import kgame5.pbe.atlantic.helper.PBEConfig;
import kgame5.pbe.atlantic.vo.EntityVO;
import kgame5.pbe.engine.components.TickedComponent;

import flash.utils.Dictionary;
import flash.utils.getDefinitionByName;

/**
	 * @author max
	 * 2010-12-13
	 * 并行状态机，根据fsmParam的列表来构造
	 */
	public class FSMBatchComp extends TickedComponent {
		
		public static const NAME:String="FSMBatchComp";
		
		//保持各个fsm用来遍历用,并行状态机
		private var dict_fsm : Dictionary;//dict_fsm[fsmID]=FSM
		
		private var entVO:EntityVO;
		
		//param是FSMParam的数组
		public function FSMBatchComp(vo:EntityVO,fsmIDList:Array) {
			//T.p("FSMBatchComp");
			dict_fsm = new Dictionary();
			
			entVO=vo;
			
			//根据参数构造fsm
			for each(var fsmID:String in fsmIDList){
				var fsm:FSM=new FSM(fsmID,vo,this);
				var fsmInfoXML:XML=PBEConfig.getFSMXML(fsmID);
				//T.p(fsmID,fsmInfoXML);
				for each(var stateXML:XML in fsmInfoXML.state){
					//T.p(stateXML.@id,stateXML.@clazz);
					var stateClassName:String=stateXML.@clazz;
					var StateClass:Class=getDefinitionByName(stateClassName) as Class;
					var s:GState=new StateClass();
					
					fsm.addState(s);
				}
				dict_fsm[fsm.getID()]=fsm;
				
			}
		}
		//为每个fsm设置eventDipatcher,以便发送事件
		override protected function onAdd() : void {
			super.onAdd();
			
			for each(var fsm:FSM in dict_fsm){
				fsm.setEventDispatcher(owner.eventDispatcher);
			}
		}
		
		//更新状态机
		override public function onTick(deltaTime : Number) : void {
			if(_isRunning){
				for each(var fsm:FSM in dict_fsm) {
					fsm.update();
				}
			}
		}
		
		private var _isRunning:Boolean=true;
		
		public function pause():void{
			//T.p("pause");
			_isRunning=false;
			entVO.spcVO.running=false;
		}

		public function play():void{
			//T.p("play");
			_isRunning=true;
			entVO.spcVO.running=true;
		}
		public function isRunning():Boolean{
			return _isRunning;
		}
		
		//切换某个fsm到特定状态
		public function SwitchFSMState(fsmID : String,stateID : String,data : Object = null,followingStateParams:FollowingStateParams=null) : void {
			var fsm : FSM = findFSM(fsmID);
			if(fsm != null) {
				fsm.SwitchState(stateID, data,followingStateParams);
			}else{
				throw new Error("无此fsm定义",fsmID);
			}
		}
		
		//取得某状态机当前状态，这个是有必要的，当有外界输入的时候，如鼠标点了一下鱼，
		//controllerComp的逻辑需要判断一下如果是特定状态才执行某动画
		public function getFSMCurrStateID(fsmID:String):String{
			var fsm:FSM=findFSM(fsmID);
			if(fsm==null){
				return null;
			}else{
				return fsm.getCurState().getID();//可能报空
			}
		}
		
		//很实用的函数，判断某状态机是否处于某状态
		//这个用来解决状态机之间的协作用
		public function isCurrState(testFsmID:String,testStateID:String):Boolean{
			var currStateID:String=getFSMCurrStateID(testFsmID);
			return currStateID==testStateID;
		}
		
		//-
		private function findFSM(fsmID : String) : FSM {
			return dict_fsm[fsmID];
		}
		
		//-很实用的两个函数，由ViewComp通知Controller而传入这里，最终由当前GState处理
		//x,y是此mc的x,y
		public function click(x:int,y:int):void{
			for each(var fsm:FSM in dict_fsm){
			
				fsm.getCurState().onClick(x,y);
			}
		}
		
		public function endOfFrame(animID:String):void{
			for each(var fsm:FSM in dict_fsm){
				
				fsm.getCurState().onEndOfFrame(animID);
			}
		}
		
		public function rollOver():void{
			for each(var fsm:FSM in dict_fsm){
				
				fsm.getCurState().onRollOver();
			}
		}
		public function rollOut():void{
			for each(var fsm:FSM in dict_fsm){
				
				fsm.getCurState().onRollOut();
			}
		}
	}//end of class
}

//		//==================操作FSMS=================
//		//子类如果需要fsm通知guActor,可以覆写此函数
//		//注意，添加时fsm需要向Entity注册监听事件
//		private function addFSM(fsm : FSM) : FSM {
//			if(dict_fsm[fsm.getID()] != null) {
//				throw new Error("已含有此fsm");
//			}
//			dict_fsm[fsm.getID()] = fsm;
//			return fsm;
//
//		}		
//		override protected function onAdd() : void {
//			super.onAdd();
//		}
//
//		override protected function onRemove() : void {
//			super.onRemove();
//		}
//
//		override protected function onReset() : void {
//			super.onReset();
//		}

		


		//从没用到过
		//	public function removeFSM(fsmID:int):void{
		//		delete dict_fsm[fsmID];
		//	}
		//这个在各个Entity里设置
//		fsm.addEventListener(FSMEvent.TYPE, onFSMEvent);
//		fsm.addEventListener(SendCommandEvent.TYPE, onSendCommandEvent);
//		
//		//暂存一下，方便以后传递到State中去
//		fsm.setVO(this.vo);
//		fsm.setHardVO(this.hard);
		//	//设置到各个state里的ISceneWorld
		//	public function setSenceWorld(senceWorld:ISenceWorld):void{
		//		for each(var fsm:FSM in dict_fsm){
		//			fsm.setSenceWorld(senceWorld);
		//		}
		//	}
		//	//当Entity被添加到世界后，被world调用，传入
		//	public function setSpacialManager(spacialManager:SpacialManager):void{
		//		for each(var fsm:FSM in dict_fsm){
		//			fsm.setSpacialManager(spacialManager);
		//		}
		//	}